﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Host.RemoteHandler;
using DWF.Studio.RemoteCtrls;
using DWF.Studio.RemoteCtrls.Proxy;
using DWF.Studio.Script;
using System;
using System.AddIn.Pipeline;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DWF.Studio.Host
{
    /// <summary>
    /// 远程工具栏本地宿主
    /// </summary>
    internal class RemoteToolBoxHost : IDisposable
    {
        /// <summary>
        /// 远程活动工具栏代理
        /// </summary>
        private ActivityToolBoxProxy _ctrlToolBoxProxy = null;

        /// <summary>
        /// 自定义活动库名
        /// </summary>
        private string[] _customActivityNames = null;

        /// <summary>
        /// 工具栏
        /// </summary>
        private FrameworkElement _activtyToolBox = null;

        private GenericHandler<LoadInfoEventArgs> _handler = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="toolbox"></param>
        /// <param name="customActivityNames"></param>
        public RemoteToolBoxHost(ActivityToolBoxProxy toolbox, string[] customActivityNames)
        {
            _ctrlToolBoxProxy = toolbox;
            _customActivityNames = customActivityNames;

            _handler = new GenericHandler<LoadInfoEventArgs>();
            _handler.SetAction(loadinfo);
            _ctrlToolBoxProxy.ActivityLoading += _handler.Action;
        }

        private void loadinfo(LoadInfoEventArgs e)
        {
            Views.WindowLoading.SetMsg(e.LoadInfo);
        }

        /// <summary>
        /// 释放工具栏
        /// </summary>
        public void Dispose()
        {
            _ctrlToolBoxProxy.Dispose();
            _handler.Dispose();
            this._handler = null;
            this._ctrlToolBoxProxy = null;
        }

        /// <summary>
        /// 获取工具栏
        /// </summary>
        /// <returns></returns>
        public FrameworkElement GetToolBox()
        {
            if (_activtyToolBox == null)
            {
                this._activtyToolBox = FrameworkElementAdapters.ContractToViewAdapter(_ctrlToolBoxProxy.GetToolBox(_customActivityNames));
            }

            return _activtyToolBox;
        }
    }
}
